﻿using System;
using System.Disposables;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using SoftSource.Common;
using SoftSource.MVVMC;
using SoftSource.Timecard.ViewModelInterfaces;

namespace SoftSource.Timecard.Views
{
    public partial class GetUserPasswordView : UserControl, IDisposable, IView<IGetUserPasswordViewModelConsume>
    {
        #region Fields
        private CompositeDisposable compositeDisposable;
        #endregion Fields

        public GetUserPasswordView()
        {
            InitializeComponent();

            this.compositeDisposable = new CompositeDisposable();
        }

        #region Events

        private void GetPassword_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                this.Cancel_Click(this.CancelButton, new RoutedEventArgs());
            }
            else if (e.Key == Key.Enter && this.OKButton.IsEnabled)
            {
                this.ViewModel.Password = passwordBox.Password;
                this.OK_Click(this.OKButton, new RoutedEventArgs());
            }
        }

        private void OK_Click(object sender, RoutedEventArgs e)
        {
            if (this.ViewModel != null)
            {
                this.ViewModel.OK.TryInvoke();
            }
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            if (this.ViewModel != null)
            {
                this.ViewModel.Cancel.TryInvoke();
            }
        }

        #endregion Events

        #region IView<IGetUserPasswordViewModelConsume> Members

        public IGetUserPasswordViewModelConsume ViewModel
        {
            get { return this.DataContext as IGetUserPasswordViewModelConsume; }
            set { this.DataContext = value; }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            this.compositeDisposable.Dispose();
            GC.SuppressFinalize(this);
        }

        #endregion

        ~GetUserPasswordView()
        {
            this.Dispose();
        }
    }
}
